package com.projectzqjz.parttimework.utils;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.projectzqjz.parttimework.App;
import com.projectzqjz.parttimework.entity.PartBaseEntity;
import com.projectzqjz.parttimework.entity.UserLocationBo;
import com.projectzqjz.parttimework.myinterface.myLocation;
import com.projectzqjz.parttimework.net.MyCallback;
import com.projectzqjz.parttimework.net.Task;

import retrofit2.Response;


public class GPSUtils {
    @SuppressLint("StaticFieldLeak")
    private static GPSUtils gpsUtils = null;
    private Context mContext;
    private AMapLocationClient mLocationClient;
    private myLocation location;
    private GPSUtils(Context context) {
        mContext = context;
    }

    public static GPSUtils getInstance(Context context) {
        if (gpsUtils == null) {
            synchronized (GPSUtils.class) {
                if (gpsUtils == null) {
                    gpsUtils = new GPSUtils(context);
                }
            }
        }
        return gpsUtils;

    }


    @SuppressLint("MissingPermission")
    public void getLocation(myLocation myLocation) {
        location=myLocation;
        mLocationClient = new AMapLocationClient(mContext);
        //初始化定位参数
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mLocationClient.setLocationListener(aMapLocationListener);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        mLocationOption.setOnceLocation(true);
        //设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        mLocationClient.stopLocation();
        //启动定位
        mLocationClient.startLocation();
    }

    private AMapLocationListener aMapLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            int code=aMapLocation.getErrorCode();
            double latitude = aMapLocation.getLatitude();
            double longitude = aMapLocation.getLongitude();
            if (code==0){//定位成功
                String cityCode=aMapLocation.getCityCode();
                String currcity = aMapLocation.getCity();
                location.onSuccess(currcity);
                String district = aMapLocation.getDistrict();
                String currnation = aMapLocation.getCountry();
                String currprovince = aMapLocation.getProvince();
                UserLocationBo userLocationBo = new UserLocationBo();
                userLocationBo.setAppid(SPUtils.get(App.getContext(), "uniqueIdentifier", "android") + "");
                userLocationBo.setCurrcity(currcity);
                userLocationBo.setCurrdistrict(district);
                userLocationBo.setCurrnation(currnation);
                userLocationBo.setCurrprovince(currprovince);
                userLocationBo.setLatitude(latitude);
                userLocationBo.setLongitude(longitude);
                userLocationBo.setLocation(aMapLocation.getAddress());
                int userId=Integer.parseInt(SPUtils.get(App.getContext(), "userId", "")+"");
                userLocationBo.setUserid(userId);
                upLoadLocation(userLocationBo);
            }else {//定位失败
                //定位失败
                location.onFail(code);
            }

        }
    };

    private void upLoadLocation(UserLocationBo userLocationBo) {
        Task.getApiService().upLoadLocation(userLocationBo).enqueue(new MyCallback<PartBaseEntity>((Activity) mContext) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
//                Log.e("uploadLocation", "上传位置信息！" );
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });

    }
}